package com.zhenyi.fenda.controller;

import java.util.Date;
import java.util.List;

import org.hashids.Hashids;

import com.cenjiawen.common.IndexController;
import com.fenda.common.model.FdAnswer;
import com.fenda.common.model.FdConsult;
import com.fenda.common.model.FdMaster;
import com.fenda.common.model.FdOrder;
import com.fenda.common.model.FdOrderComment;
import com.fenda.common.model.FdQuestion;
import com.fenda.common.model.FdUser;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jfinal.weixin.sdk.kit.IpKit;
import com.utils.AuthUtils;

import cn.hutool.core.date.DateUtil;

public class ApiController extends IndexController {

	private final int pageSize = 50;

	/**
	 * 大师列表
	 */
	public void master() {
		SqlPara sqlPara = Db.getSqlPara("FDFindMasters");
		Page<Record> pages = Db.paginate(getPage(), pageSize, sqlPara);
		setItems(pages.getList());
		renderPage(pages);
	}

	/**
	 * 用户个人信息
	 */
	public void userInfo() {
		Long userId = getParaToLong("userId", 5L);
		SqlPara sqlPara = Db.getSqlPara("FdFindUser", userId);
		FdUser user = FdUser.dao.findFirst(sqlPara);
		renderResult(user);
	}

	/**
	 * 大师个人信息
	 * 
	 * @Time 2018年7月18日 下午10:46:36
	 */
	public void masterInfo() {
		Kv kv = Kv.create();
		Integer userId = getParaToInt("userId");
		if (userId != null) {
			kv.set("userId", userId);
		}
		SqlPara sqlPara = Db.getSqlPara("FDFindMasters", kv);
		FdMaster master = FdMaster.dao.findFirst(sqlPara);
		SqlPara tagSql = Db.getSqlPara("FindMasterTag", master.getUserId());
		List<Record> tags = Db.find(tagSql);
		master.put("tags", tags);
		renderSuccess(master);
	}

	public void home() {
		SqlPara sqlPara = Db.getSqlPara("FdFindAdv", 1);
		List<Record> type1Adv = Db.find(sqlPara);
		SqlPara hotComments = Db.getSqlPara("FdFindMasterOrderComments");
		List<Record> comments = Db.paginate(getPage(), 10, hotComments).getList();
	}

	private FdUser getUser() {
		String uid = AuthUtils.getTokenUid(this);
		if (StrKit.notBlank(uid)) {
			return FdUser.dao.findById(Integer.parseInt(uid));
		}
		return FdUser.dao.findById(5);
	}

	public FdUser getMaster() {
		return FdUser.dao.findById(1);
	}

	private Hashids orderHash = new Hashids("fdorder", 2);

	private boolean pushOrder(FdOrder order) {
		return true;
	}

	/**
	 * 生成订单
	 * 
	 * @param master
	 * @param userId
	 * @return
	 * @Time 2018年7月19日 下午9:43:06
	 */
	private FdOrder genOrder(FdMaster master, Long userId) {
		FdOrder order = new FdOrder();
		order.setUserId(userId);
		order.setPrice(master.getPrice());
		order.save();
		Long orderId = order.getId();
		String no = DateUtil.format(new Date(), "YYYYMMddhhmmss") + orderHash.encode(orderId);
		order.setOrderNo(no);
		order.update();
		boolean flag = pushOrder(order);
		if (!flag) {
			renderFailure();
			return null;
		}
		return order;
	}

	/**
	 * 提一个问题+下单支付
	 */
	public void askQuestion() {
		Long userId = getUser().getId();
		Long masterId = getParaToLong("masterId", 1L);
		FdMaster master = FdMaster.dao.findById(masterId);
		String content = getPara("content", "我要提问,我是个好人吗?");
		String title = getPara("title", "长得好看是什么体验");
		FdOrder order = genOrder(master, userId);
		if (order == null) {
			renderFailure();
			return;
		}
		Long orderId = order.getId();
		FdQuestion fdQuestion = new FdQuestion();
		fdQuestion.setTitle(title);
		fdQuestion.setContent(content);
		fdQuestion.setUserId(userId);
		fdQuestion.setOrderId(orderId);
		fdQuestion.setMasterId(masterId);
		fdQuestion.save();
		renderResult(order);
	}

	/**
	 * 回答问题
	 * 
	 * @Time 2018年7月19日 下午9:42:57
	 */
	public void answer() {
		Long userId = getUser().getId();
		Long questionId = getParaToLong("questionId", 7L);
		String content = getPara("content", "好看就是牛逼");
		FdQuestion question = FdQuestion.dao.findById(questionId);
		if (question.getUserId().equals(userId)) {
			renderFailure();
			return;
		}
		FdAnswer answer = new FdAnswer();
		answer.setUserId(userId);
		answer.setQuestionId(questionId);
		answer.setContent(content);
		answer.save();

		SqlPara sqlPara = Db.getSqlPara("FdFindQuestionAnswersSimple", questionId);
		List<Record> items = Db.find(sqlPara);
		question.setAnswerCount(items.size());
		question.update();
		renderSuccess();
	}

	/**
	 * 提问咨询
	 */
	public void addConsultQuestion() {
		Long userId = getUser().getId();
		Long masterId = getParaToLong("masterId", 1L);
		FdMaster master = FdMaster.dao.findById(masterId);
		if (master == null) {
			renderFailure();
			return;
		}
		FdOrder order = genOrder(master, userId);
		Long orderId = order.getId();
		String content = getPara("content", "我要提问,我是个好人吗?");
		String topic = getPara("topic", "长得好看是什么体验");
		FdConsult consult = new FdConsult();
		consult.setContent(content);
		consult.setTopic(topic);
		consult.setMasterId(masterId);
		consult.setUserId(userId);
		consult.setOrderId(orderId);
		consult.save();
		renderSuccess();
	}

	/**
	 * 订单列表
	 * 
	 * @Time 2018年7月19日 下午9:42:47
	 */
	public void orders() {
		Long userId = getUser().getId();
		Kv kv = Kv.by("userId", userId);
		Integer status = getParaToInt("status");
		if (status != null) {
			kv.set("status", status);
		}
		SqlPara sqlPara = Db.getSqlPara("FdFindOrders", kv);
		List<Record> items = Db.find(sqlPara);
		renderSuccess(items);
	}

	/**
	 * 评论订单
	 * 
	 * @Time 2018年7月19日 下午9:42:40
	 */
	public void commentOrder() {
		FdUser user = getUser();
		String orderNo = getPara("orderNo");
		FdOrder order = FdOrder.dao.findByOrderNo(orderNo);
		if (!order.getUserId().equals(user.getId())) {
			renderFailure();
			return;
		}
		Long orderId = order.getId();
		String content = getPara("content");
		FdOrderComment orderComment = new FdOrderComment();
		orderComment.setContent(content);
		orderComment.setOrderId(orderId);
		orderComment.save();
		renderSuccess();
	}

	/**
	 * (所有)用户提出的问题,IMG_8580
	 */
	public void questions() {
		SqlPara sqlPara = Db.getSqlPara("FdFindQuestion");
		List<Record> items = Db.paginate(getPage(), pageSize, sqlPara).getList();
		renderSuccess(items);
	}

	/**
	 * 大师要回答的问题
	 */
	public void masterQuestions() {
		Long masterId = getMaster().getId();
		Kv kv = Kv.by("masterId", masterId);
		SqlPara sqlPara = Db.getSqlPara("FdFindQuestion", kv);
		List<Record> items = Db.paginate(getPage(), pageSize, sqlPara).getList();
		renderSuccess(items);
	}

	/**
	 * 大师咨询的评论
	 */
	public void masterComments() {
		Long userId = getParaToLong("userId", 1L);
		Kv kv = Kv.by("userId", userId);
		SqlPara sqlPara = Db.getSqlPara("FdMasterConsultComment", kv);
		List<Record> items = Db.find(sqlPara);
		renderResult(items);
	}

	/**
	 * 用户自己的问题,可以根据订单状态查询
	 */
	public void userQuestions() {
		Long userId = getUser().getId();
		Kv kv = Kv.by("userId", userId);
		Integer status = getParaToInt("status");
		if (status != null) {
			kv.set("status", status);
		}
		SqlPara sqlPara = Db.getSqlPara("FdFindQuestion", kv);
		List<Record> items = Db.paginate(getPage(), pageSize, sqlPara).getList();
		renderSuccess(items);
	}

	/**
	 * 问题详情
	 */
	public void questionInfo() {
		Long questionId = getParaToLong("questionId", 6L);
		Kv kv = Kv.by("questionId", questionId);
		SqlPara sqlPara = Db.getSqlPara("FdFindQuestion", kv);
		Record record = Db.findFirst(sqlPara);

		SqlPara sqlParaAnswers = Db.getSqlPara("FdFindQuestionAnswers", questionId);
		List<FdAnswer> items = FdAnswer.dao.find(sqlParaAnswers);
		Long answerId = record.getLong("answerId");
		if (answerId != null) {
			for (FdAnswer answer : items) {
				if (answerId.equals(answer.getId())) {
					answer.setIsAnswer(1);
				}
			}
		}
		record.set("answer", items);
		FdQuestion question = FdQuestion.dao.findById(questionId);
		question.setVisitCount(question.getVisitCount() + 1);
		question.update();
		renderSuccess(record);
	}

	public void register() {
		String name = getPara("name");
		FdUser user = new FdUser();
		user.setName(name);
		user.save();
		renderSuccess();
	}

	public void login() {
		Long userId = getParaToLong("userId,", 5L);
		FdUser user = FdUser.dao.findById(userId);
		if (user == null) {
			renderFailure();
			return;
		}
		AuthUtils.login("" + userId);
		renderSuccess();
	}

	@Override
	public void index() {
		String ip = IpKit.getRealIpV2(getRequest());
		System.out.println(ip);
		renderSuccess();
	}

}
